/**
 * Created by ray on 15/4/21.
 */
define(function (require, exports, module) {
    require('jquery');
    window._initMods = {};

    if (!window._jsInited) {
        window._jsInited = true;
        $(function () {
            window.__callback = {}; //专用于各个模块之间相互调用
            $('*[data-fn]').each(function () {
                Init.initDataFunc($(this));
            });
            //var scope = require('ui/scope');
            //scope.apply();
        });
    }

    var Init = {
        objId: 0,
        initDataFunc: function (obj) {
            if (!obj.attr('data-fn')){
                return false;
            }
            var func = obj.attr('data-fn').split(' ');
            if (!obj.attr('id')) {
                var _id = "_fn_mark_id_" + this.objId;
                obj.attr('id', _id);
                this.objId++;
            }
            for (var i in func) {
                this.run(func[i], obj);
            }
        },
        run: function (funcName, obj, funcObj) {
            funcName = funcName.split('-');
            if (funcName[0] != 'self') {
                funcName.unshift('core');
            }
            if (!funcName[3]) {
                funcName[3] = 'init';
            }
            var file = funcName[0] + "/" + funcName[1] + "/" + funcName[2];
            if (!window._initMods[file]) {
                window._initMods[file] = [null, 'init', [[funcName[3], obj]]]; // 初始化的类 状态 初始化过程中等待执行的队列
                require.async(file, function (Mod) {
                    var _mod;
                    window._initMods[file][0] = Mod;
                    window._initMods[file][1] = 'initFinish';
                    for (var j in window._initMods[file][2]) {
                        try {
                            _mod = new Mod(window._initMods[file][2][j][1]);
                            _mod[window._initMods[file][2][j][0]]();
                        }catch (e){
                            console.log("[ERROR] "+funcName.join('-'), e);
                        }
                    }
                });
            } else if (window._initMods[file][1] == 'init') {
                window._initMods[file][2].push([funcName[3], obj]);
            } else {
                var Mod = window._initMods[file][0];
                var _mod = new Mod(obj);
                try {
                    if (funcObj) {
                        _mod[funcName[3]](funcObj);
                    } else {
                        _mod[funcName[3]]();
                    }
                } catch (e) {
                    console.log(e);
                }
            }
        },
        funcCallback: function (callback) {
            var funcObj = callback.split(',');
            for (var i in funcObj) {
                if (funcObj[i].indexOf(':') > 0) {
                    funcObj[i] = funcObj[i].split(':');
                    this.run(funcObj[i][0], funcObj[1] ? $('#' + funcObj[i][1]) : null, funcObj[1] ? $('#' + funcObj[i][1]) : null);
                } else {
                    this.initDataFunc($('#' + funcObj[i]));
                }
            }
        }
    }
    module.exports = Init;
});